#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// 定义一个表示联系人的结构体
struct Contact {
    string name;
    string phone;
    string email;

    // 用于比较联系人姓名的重载运算符
    bool operator==(const string& name) const {
        return this->name == name;
    }
};

// 显示菜单
void showMenu() {
    cout << "\nAddress Book Menu:" << endl;
    cout << "1. Add Contact" << endl;
    cout << "2. Delete Contact" << endl;
    cout << "3. Find Contact" << endl;
    cout << "4. List Contacts" << endl;
    cout << "5. Exit" << endl;
    cout << "Enter your choice: ";
}

// 添加联系人
void addContact(vector<Contact>& contacts) {
    Contact newContact;
    cout << "Enter name: ";
    getline(cin >> ws, newContact.name);
    cout << "Enter phone: ";
    getline(cin, newContact.phone);
    cout << "Enter email: ";
    getline(cin, newContact.email);

    contacts.push_back(newContact);
    cout << "Contact added successfully." << endl;
}

// 删除联系人
void deleteContact(vector<Contact>& contacts) {
    string name;
    cout << "Enter the name of the contact to delete: ";
    getline(cin >> ws, name);

    auto it = find_if(contacts.begin(), contacts.end(), [&](const Contact& c) { return c.name == name; });
    if (it != contacts.end()) {
        contacts.erase(it);
        cout << "Contact deleted successfully." << endl;
    } else {
        cout << "Contact not found." << endl;
    }
}

// 查找联系人
void findContact(const vector<Contact>& contacts) {
    string name;
    cout << "Enter the name of the contact to find: ";
    getline(cin >> ws, name);

    auto it = find_if(contacts.begin(), contacts.end(), [&](const Contact& c) { return c.name == name; });
    if (it != contacts.end()) {
        cout << "Name: " << it->name << endl;
        cout << "Phone: " << it->phone << endl;
        cout << "Email: " << it->email << endl;
    } else {
        cout << "Contact not found." << endl;
    }
}

// 列出所有联系人
void listContacts(const vector<Contact>& contacts) {
    if (contacts.empty()) {
        cout << "No contacts available." << endl;
    } else {
        for (const auto& contact : contacts) {
            cout << "Name: " << contact.name << ", Phone: " << contact.phone << ", Email: " << contact.email << endl;
        }
    }
}

int main() {
    vector<Contact> contacts;
    int choice;

    do {
        showMenu();
        cin >> choice;

        switch (choice) {
            case 1:
                addContact(contacts);
                break;
            case 2:
                deleteContact(contacts);
                break;
            case 3:
                findContact(contacts);
                break;
            case 4:
                listContacts(contacts);
                break;
            case 5:
                cout << "Exiting the program." << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 5);

    return 0;
}